package com.example.androidcompletesec.clipboard;

import com.example.androidcompletesec.R;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.net.Uri;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class DragButton extends Activity {
	Button btnSource;
	Button btnTarget;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dragbutton);
		
		btnSource = (Button) findViewById(R.id.source);
		btnSource.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				ClipData clip = ClipData.newPlainText("dragtext", "dragtext");
				v.startDrag(clip, new View.DragShadowBuilder(v), null, 0);
				return false;
			}
		});
		
		Button btnSource2 = (Button) findViewById(R.id.source2);
		btnSource2.setOnLongClickListener(new View.OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				ClipData clip = ClipData.newRawUri("uri", Uri.parse("content://com.example.androidcompletesec.cp.EnglishWord/word/boy"));
				v.startDrag(clip, new View.DragShadowBuilder(v), null, 0);
				return false;
			}
		});
		btnTarget = (Button) findViewById(R.id.target);
		btnTarget.setOnDragListener(mDragListener);
	}
	View.OnDragListener mDragListener = new View.OnDragListener() {
		
		@Override
		public boolean onDrag(View v, DragEvent event) {
			Button btn;
			if(v instanceof Button){
				btn = (Button) v;
			}else{
				return false;
			}
			
			switch(event.getAction()){
			case DragEvent.ACTION_DRAG_STARTED:
				if(event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)){
					btn.setText("Drop OK");
					return true;
				}else{
					return false;
				}
			case DragEvent.ACTION_DRAG_ENTERED:
				btn.setText("Enter");
				return true;
			case DragEvent.ACTION_DRAG_EXITED:
				btn.setText("Exit");
				return true;
			case DragEvent.ACTION_DROP:
				String text = event.getClipData().getItemAt(0).getText().toString();
				btn.setText(text);
				return true;
			case DragEvent.ACTION_DRAG_ENDED:
				if(event.getResult()){
					Toast.makeText(DragButton.this, "Drag & Drop completed", 0).show();
				}else{
					btn.setText("Target");
				}
				return true;
			}
			return true;
		}
	};
}
